package com.mm.fam.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.fam.common.utils.LoggerUtils;
import com.mm.fam.web.base.CommonConstant;
import com.mm.fam.web.base.ResponseModel;
import com.mm.fam.web.base.ResponseUtil;
import com.mm.fam.web.entity.Gateway;
import com.mm.fam.web.service.GatewayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dhl
 */

@Api(value="节点模块")
@RestController
@RequestMapping(value="/api/gateway")
public class GatewayController {
    @Autowired
    private GatewayService gatewayService;

    @GetMapping(value = "/list")
    @ApiOperation(value="分页查询网关列表", notes="分页查询网关列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", required=false, name = "pageNo", value = "页码", dataType = "int"),
            @ApiImplicitParam(paramType="query", required=false, name = "pageSize", value = "页数", dataType = "int")
    })
    public ResponseEntity<ResponseModel<IPage<Gateway>>> list(
            @RequestParam(required = false, name = "pageNo") Integer pageNo,
            @RequestParam(required = false, name = "pageSize") Integer pageSize){
        ResponseEntity<ResponseModel<IPage<Gateway>>> re = null;
        try{
            List<Gateway> list = gatewayService.getGatewayList();
            IPage<Gateway> page = new Page(pageNo,pageSize);
            page.setRecords(list);
            re = ResponseUtil.success(page);
        }catch(Exception e){
            LoggerUtils.error(getClass(), "[GatewayController list]" + e.getMessage());
            re = ResponseUtil.error(CommonConstant.Message.OPTION_FAILURE);
        }
        return re;
    }

    @PostMapping(value = "/save")
    @ApiOperation(value="保存主账户", notes="保存主账户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="body", required=true, name = "gateway", value = "主账户对象", dataType = "Gateway")
    })
    public ResponseEntity<ResponseModel<Object>> save(@RequestBody Gateway gateway){
        ResponseEntity<ResponseModel<Object>> re = null;
        try{
            gatewayService.upsertGatewayByGatewayId(gateway);
            re = ResponseUtil.success("");
        }catch(Exception e){
            LoggerUtils.error(getClass(), "[GatewayController save]" + e.getMessage());
            re = ResponseUtil.error(CommonConstant.Message.OPTION_FAILURE);
        }
        return re;
    }

    @Deprecated
    @GetMapping(value = "/delete")
    @ApiOperation(value="删除主账户", notes="删除主账户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", required=false, name = "gatewayId", value = "gatewayId", dataType = "string")
    })
    public ResponseEntity<ResponseModel<Object>> delete(@RequestParam(required = false, name = "gatewayId") String gatewayId) {
        ResponseEntity<ResponseModel<Object>> res = null;
        try {
            gatewayService.deleteGatewayByGatewayId(gatewayId);
            res = ResponseUtil.success("删除成功");
        } catch (Exception e) {
            LoggerUtils.error(getClass(), "主账户删除失败:" + e.getMessage());
        }
        return res;
    }
}
